﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
   public  class LensDAO
    {
       CameraDBEntities db = new CameraDBEntities();
        public List<Lens> GetListAll()
        {
            return db.Lenses.Where(x => x.lensStatus == true).ToList();
        }
        public List<Lens> GetListAll_Sort()
        {
            return db.Lenses.Where(x => x.lensStatus == true).OrderByDescending(x=>x.lensId).ToList();
        }
        public List<Lens> GetListTop10()
        {
            return db.Lenses.Where(x => x.lensStatus == true).Take(10).ToList();
        }
        public Lens GetById(int id)
        {
            return db.Lenses.Where(x => x.lensId == id && x.lensStatus == true).SingleOrDefault();
        }

        public List<Lens> GetListSearch(string tukhoa)
        {
            List<Lens> listkq = new List<Lens>();
            listkq = db.Lenses.Where(x => x.lensStatus == true).ToList();
            listkq = listkq.FindAll(

                delegate(Lens math)
                {
                    if (Utils.ConvertToUnSign(math.lensNames.ToLower()).Contains(tukhoa))
                        return true;
                    else
                        return false;
                }
            );
            return listkq;
        }

        public bool Insert(Lens c)
        {
            try
            {
                db.Lenses.Add(c);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit(Lens ca)
        {
            try
            {
                Lens a = new Lens();
                a = db.Lenses.Where(x => x.lensId == ca.lensId).SingleOrDefault();
                a.lensNames = ca.lensNames;
                a.lensImages = ca.lensImages;
                a.lensPrice = ca.lensPrice;
                a.lensDetails = ca.lensDetails;
                a.lensFocallensgth = ca.lensFocallensgth;
                a.lensMaxaperture = ca.lensMaxaperture;
                a.lensMinaperture = ca.lensMinaperture;
                a.lensMinfocus = ca.lensMinfocus;
                a.lensMaxmagnification = ca.lensMaxmagnification;
                a.lensStatus = ca.lensStatus;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                Lens a = new Lens();
                a = db.Lenses.Where(x => x.lensId == id).SingleOrDefault();
                db.Lenses.Remove(a);


                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
